#include "loginwindow.h"

LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent)
{
    initLoginWindow();
}

void LoginWindow::initLoginWindow()
{
    resize(600,400);
    setWindowTitle("用户登录");
    setWindowIcon(QIcon(":/new/prefix1/img/login.jpg"));

    QPalette palette;
    palette.setColor(QPalette::Base, Qt::blue);

    formLayout = new QFormLayout;
    mainLayout = new QVBoxLayout(this);
    leftLayout = new QHBoxLayout;
    rightLayout = new QVBoxLayout;
    modifyLayout = new QVBoxLayout;
    loginLayout = new QHBoxLayout;
    smallLayout = new QHBoxLayout;

    edit = new QTextEdit;
    label = new QLabel("当前用户：",this);
    nameLineEdit = new QLineEdit;
    nameLineEdit->setPlaceholderText("请输入用户名");
    passwdLineEdit = new QLineEdit;
    passwdLineEdit->setPlaceholderText("请输入密码");

    pushButton = new QPushButton("<<<用户须知");
    btn1 = new QPushButton("登录");
    btn2 = new QPushButton("注销");
    btn3 = new QPushButton("关闭");
    modifyName = new QPushButton("修改口令");
    modifyPwd = new QPushButton("修改用户");

    //加载静态图片
    img = QPixmap(":/new/prefix1/img/user.jpg");
    img = img.scaled(200,200);
    label->setPixmap(img);

    formLayout->addRow(tr("&账户:"), nameLineEdit);
    formLayout->addRow(tr("&密码:"), passwdLineEdit);
    loginLayout->addWidget(btn1);
    loginLayout->addWidget(btn2);
    loginLayout->addWidget(btn3);
    modifyLayout->addWidget(modifyName);
    modifyLayout->addWidget(modifyPwd);

    // 左侧布局，主要放置用户头像等信息
    leftLayout->addWidget(label);
    // 右侧上方布局，包含账户密码输入框以及修改相关按钮
    smallLayout->addLayout(formLayout);
    smallLayout->addLayout(modifyLayout);
    // 右侧下方布局，包含登录、注销等操作按钮
    rightLayout->addLayout(smallLayout);
    rightLayout->addLayout(loginLayout);
    // 将右侧布局添加到左侧布局中，整体构成窗口主要布局部分
    leftLayout->addLayout(rightLayout);
    mainLayout->addLayout(leftLayout);
    mainLayout->addWidget(pushButton);
    mainLayout->addWidget(edit);


    connect(pushButton,SIGNAL(clicked(bool)),SLOT(pushButton_clicked()));
    connect(btn3,SIGNAL(clicked(bool)),SLOT(close()));
    connect(btn2,SIGNAL(clicked(bool)),SLOT(saveUserInfo()));

}
void LoginWindow::pushButton_clicked()
{
    edit->setVisible(!edit->isVisible());
}

void LoginWindow::saveUserInfo()
{
    QFile file("./cookies");
    file.open(QIODevice::WriteOnly);
    file.write(nameLineEdit->text().toUtf8() +","+ passwdLineEdit->text().toUtf8());
}
